  <?php
    /**
	 * 获取系统任务（奖券 任务）奖励
	 */
    class CoinTaskGetReward extends APIBase {
		
		public $tag = "CoinTaskGetReward";
		public $isLogin = true;
		
		public function before() {	
			$this->initCacheRedis();		
			return true;
		}

		public function logic() 
		{	
			//奖励序号
			if (!isset($this->param['reward_no']))
			{
				$this->returnError(403, "没有设置领奖序号");
			}
			else
			{
				$reward_no = (int)$this->param['reward_no'];				
			}
			
			$rets = $this->data_redis->hMget( "hu:{$this->uid}", array('isget_coin_task_reward_record','coin_task_complete_record'));
			
			$isget_coin_task_reward_record = (int)$rets['isget_coin_task_reward_record'];
			$coin_task_complete_record = $rets['$coin_task_complete_record'];
			
			//从缓存中获取奖券任务总数		
			$task_sum = count($this->cache_redis->keys("hcointask:*"));
			//累计局数奖励		
			if ( ($reward_no < 0) || ($reward_no > $task_sum ))
			{
				$this->returnError(404, "领奖序号错误");
			}
			
			$task_info = $this->cache_redis->hgetall("hcointask:{$reward_no}");
				
			//如果已经达到领奖条件，并且没有领过，则可以领奖
			if ( true && ( ($isget_coin_task_reward_record & (1 << $reward_no-1)) == 0) )
			{
					$reward_coin = $task_info['task_reward_coin'];					//领奖成功
					$isget_coin_task_reward_record = $isget_coin_task_reward_record | (1 << $reward_no-1);		//记录领奖记录
			}
			else 
			{
					$this->returnError(405, "不能领取该序号的奖励");
			}
			
			//奖励后金币数量			
			$coin = $this->data_redis->hincrBy("hu:{$this->uid}", "coin", $reward_coin);
			$this->data_redis->hMset("hu:{$this->uid}",array("isget_coin_task_reward_record" => $isget_coin_task_reward_record) );
					
			$result = array();
			$result['reward_coin'] = (int)$reward_coin;		//奖励奖券金额
			$result['cur_coin'] = (int)$coin;
			
			$this->returnData($result, 0, "领取奖励成功");
		}
		
    	public function after() {
    		$this->deinitCacheRedis();
			$this->deinitDataRedis();
		}
    }
?>